﻿using Dqs_BLL;
using Model;
using Sunny.UI;

namespace Dqs_UI
{
	public partial class frmLogin : UILoginForm
	{
		public static StudentInfo LoginStudentInfo;
		public static TeacherInfo LoginTeacherInfo;
		public static AdminInfo LoginAdminInfo;
		public frmLogin()
		{
			InitializeComponent();
		}
		private Admin_BLL admin_BLL = new Admin_BLL();
		private Student_BLL student_BLL = new Student_BLL();
		private Teacher_BLL teacher_BLL = new Teacher_BLL();
		public Object UserInfo { get; set; }


		/// <summary>
		/// 登录用户类别 1 -- 管理员 2 -- 教师 3 -- 学生
		/// </summary>
		public int LoginType { get; set; }

		private void rbtnAdmin_CheckedChanged(object sender, EventArgs e)
		{
			if (rbtnAdmin.Checked)
			{
				this.LoginType = 1;
			}
		}


		private void rbtnTeacher_CheckedChanged(object sender, EventArgs e)
		{
			if (rbtnTeacher.Checked)
			{
				this.LoginType = 2;
			}
		}

		private void rbtnStudent_CheckedChanged(object sender, EventArgs e)
		{
			if (rbtnStudent.Checked)
			{
				this.LoginType = 3;
			}
		}


		private void frmLogin_Load(object sender, EventArgs e)
		{
			//rbtnStudent.Checked = true;
			rbtnAdmin.Checked = true;
			IsLogin = false;
		}

		/// <summary>
		/// 登录操作
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void frmLogin_ButtonLoginClick(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(UserName) || String.IsNullOrEmpty(Password))
			{
				MessageBox.Show("请输入用户名和密码");
				return;
			}
			else
			{
				if (LoginType == 1)
				{
					LoginAdminInfo = admin_BLL.IsLoginOfAdmin(UserName, Password);
					if (LoginAdminInfo != null)
					{
						UserInfo = LoginAdminInfo;
						IsLogin = true;
						this.DialogResult = DialogResult.OK;
					}
					else
					{
						MessageBox.Show("用户名或者密码错误！请重新输入");
						return;
					}
				}
				else if (LoginType == 2)
				{
					LoginTeacherInfo = teacher_BLL.IsLoginOfTeacher(UserName, Password);

					if (LoginTeacherInfo != null)
					{
						UserInfo = LoginTeacherInfo;
						IsLogin = true;
						this.DialogResult = DialogResult.OK;
					}
					else
					{
						MessageBox.Show("用户名或者密码错误！请重新输入");
						return;
					}
				}
				else if (LoginType == 3)
				{
					LoginStudentInfo = student_BLL.IsLoginOfStudent(UserName, Password);

					if (LoginStudentInfo != null)
					{
						UserInfo = LoginStudentInfo;
						IsLogin = true;
						this.DialogResult = DialogResult.OK;
					}
					else
					{
						MessageBox.Show("用户名或者密码错误！请重新输入");
						return;
					}
				}
			}
		}
	}
}
